﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace CMS.DAL
{
    public class EPioneerDataDAL
    {

        /// <summary>
        /// 更新EM_Web_News
        /// </summary>
        /// <param name="list">需要更新的ID列表</param>
        /// <param name="tyid">类型id(56/020103)</param>
        /// <param name="isPub">是否发布(0取消发布;1发布)</param>
        /// <param name="isPub">是否精选(0取消精选;1精选)</param>
        /// <returns></returns>
        public bool UpdateNews(string[] list, string tyid, string isPub, string isChos)
        {
            StringBuilder sqlStr = new StringBuilder();
            DateTime createDate = DateTime.Now;
            for (int i = 0; i < list.Length; i++)
            {
                sqlStr.Append(" if EXISTS (select * from EM_Web_News where n_id='" + list[i] + "' and n_type='" + tyid + "') begin ");
                if (string.IsNullOrEmpty(isChos))
                    sqlStr.Append(" update EM_Web_News set isPub='" + isPub + "' where n_id='" + list[i] + "' and n_type='" + tyid + "' ");
                else if (string.IsNullOrEmpty(isPub))
                    sqlStr.Append(" update EM_Web_News set n_level='" + isChos + "' where n_id='" + list[i] + "' and n_type='" + tyid + "' ");
                else
                    sqlStr.Append(" update EM_Web_News set n_level='" + isChos + "',isPub='" + isPub + "' where n_id='" + list[i] + "' and n_type='" + tyid + "' ");
                sqlStr.Append(" end else begin ");
                if (isChos == "1")
                    sqlStr.Append(" insert EM_Web_News(n_id,n_type,n_createdate,isPub,n_level) values('" + list[i] + "','" + tyid + "','" + createDate + "','1','1') ");
                else
                    sqlStr.Append(" insert EM_Web_News(n_id,n_type,n_createdate,isPub,n_level) values('" + list[i] + "','" + tyid + "','" + createDate + "','" + isPub + "','0') ");
                sqlStr.Append(" end ");
            }
            int count = SqlHelper.ExecuteNonQuery(DBConn.emoney, CommandType.Text, sqlStr.ToString());
            if (count > 0)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 更新标题
        /// </summary>
        /// <param name="id"></param>
        /// <param name="tyid"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public bool UpdateNews(string id, string tyid, string title)
        {
            StringBuilder sqlStr = new StringBuilder();
            DateTime createDate = DateTime.Now;
            sqlStr.Append(" if EXISTS (select * from EM_Web_News where n_id='" + id + "' and n_type='" + tyid + "') begin ");
            sqlStr.Append(" update EM_Web_News set n_title='" + title + "',isPub='1' where n_id='" + id + "' and n_type='" + tyid + "' ");
            sqlStr.Append(" end else begin ");
            sqlStr.Append(" insert EM_Web_News(n_id,n_type,n_createdate,isPub,n_level,n_title) values('" + id + "','" + tyid + "','" + createDate + "','1','0','" + title + "') ");
            sqlStr.Append(" end ");
            int count = SqlHelper.ExecuteNonQuery(DBConn.emoney, CommandType.Text, sqlStr.ToString());
            if (count > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 获取EM_Web_News最新数据
        /// </summary>
        /// <param name="tyid">类型id(56/020103/020107)</param>
        /// <returns></returns>
        public DataTable GetEMWebNews(string tyid)
        {
            DataTable dt = new DataTable();
            try
            {
                string sql = "select top 200 * from EM_Web_News where n_type='" + tyid + "' order by n_createdate desc";
                DataSet ds = SqlHelper.ExecuteGetDataSet(DBConn.emoney, CommandType.Text, sql);
                if (ds.Tables.Count > 0)
                    dt = ds.Tables[0];
            }
            catch
            {
                dt = null;
            }

            return dt;
        }

        /// <summary>
        /// 异动板块56
        /// </summary>
        /// <param name="strGetFields"></param>
        /// <param name="fldName"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="doCount"></param>
        /// <param name="OrderType"></param>
        /// <param name="strWhere"></param>
        /// <param name="total"></param>
        /// <param name="bz"></param>
        /// <returns></returns>
        public DataSet GetDataModelPagination(string strGetFields, string fldName, int PageSize, int PageIndex, int doCount, int OrderType, string strWhere, int total, bool bz)
        {
            DataSet ds = new DataSet();
            Tools tool = new Tools();
            tool.AddSqlParameter("strGetFields", SqlDbType.VarChar, strGetFields);
            tool.AddSqlParameter("fldName", SqlDbType.VarChar, fldName);
            tool.AddSqlParameter("PageSize", SqlDbType.Int, PageSize);
            tool.AddSqlParameter("PageIndex", SqlDbType.Int, PageIndex);
            tool.AddSqlParameter("doCount", SqlDbType.Int, doCount);
            tool.AddSqlParameter("OrderType", SqlDbType.Int, OrderType);
            tool.AddSqlParameter("strWhere", SqlDbType.VarChar, strWhere);
            try
            {
                ds = SqlHelper.ExecuteGetDataSet(DBConn.emweb_new, CommandType.StoredProcedure, "Wap_news_GetNewsName_Pagination", tool.GetSqlParameters());
            }
            catch
            {
                ds = null;
            }

            return ds;
        }

        /// <summary>
        /// 数据接口
        /// </summary>
        /// <param name="tyid"></param>
        /// <param name="title"></param>
        /// <param name="id"></param>
        /// <param name="date"></param>
        /// <param name="edate"></param>
        /// <returns></returns>
        public List<CMS.Entity.NewsList> GetNewListWhere(string tyid, string title, string id, string date, string edate)
        {
            LYM.CommonWeb.SocketClient client = new LYM.CommonWeb.SocketClient();
            string result = client.GetClientBySocket("http://172.28.1.97/news/newslist.ashx?typeid=" + tyid);
            var res = Newtonsoft.Json.JsonConvert.DeserializeObject<CMS.Entity.NewsList_Lst>(result);
            return res.list;
        }

        /// <summary>
        /// 数据接口
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public CMS.Entity.NewsEntity GetNewListWhere(string id)
        {
            LYM.CommonWeb.SocketClient client = new LYM.CommonWeb.SocketClient();
            string result = client.GetClientBySocket("http://172.28.1.97/news/unews.ashx?id=" + id);
            var res = Newtonsoft.Json.JsonConvert.DeserializeObject<CMS.Entity.NewsEntity>(result);
            return res;
        }

        /// <summary>
        /// 个股异动
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public DataTable GetSTOCKABNORMALMSG(string strWhere)
        {
            DataTable dt = new DataTable();
            try
            {
                string sql = "select * from STOCK_ABNORMAL_MSG";
                if (strWhere != "")
                    sql += strWhere;
                sql += " order by modify_time desc";
                DataSet ds = SqlHelper.ExecuteGetDataSet(DBConn.emoney, CommandType.Text, sql);
                if (ds.Tables.Count > 0)
                    dt = ds.Tables[0];
            }
            catch
            {
                dt = null;
            }

            return dt;
        }

        /// <summary>
        /// 财经新闻(内地财经)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataTable GetNEWSTEXT(string strWhere)
        {
            DataTable dt = new DataTable();
            try
            {
                string sql = "select top 200 nt.* from dbo.NEWS_TEXT as nt left join dbo.NEWS_SORT as ns on nt.TextID=ns.TextID ";
                if (strWhere != "")
                    sql += strWhere;
                sql += " order by nt.PublDate desc";
                DataSet ds = SqlHelper.ExecuteGetDataSet(DBConn.emoney, CommandType.Text, sql);
                if (ds.Tables.Count > 0)
                    dt = ds.Tables[0];
            }
            catch
            {
                dt = null;
            }

            return dt;
        }
    }
}
